package dev.ece.core.shader;

import java.io.InputStream;
import java.util.Map;

import org.lwjgl.opengl.GL20;

import dev.ece.core.texture.Texture;
import dev.ece.suit.utils.ShaderFunctions;

public class TextureCubeShader extends Shader {

	private static TextureCubeShader TEXTURE_CUBE_SHADER;
	
	public static TextureCubeShader getDefaultTextureCubeShader() {
		if(TEXTURE_CUBE_SHADER == null) {
			TEXTURE_CUBE_SHADER = new TextureCubeShader(
					TextureCubeShader.class.getClassLoader().getResourceAsStream("shaders/defer/skybox.vert"), 
					TextureCubeShader.class.getClassLoader().getResourceAsStream("shaders/defer/skybox.frag"), 
					ShaderFunctions.getFunctions());
		}
		return TEXTURE_CUBE_SHADER;
	}

	private int textureHandler;
	
	public TextureCubeShader(InputStream vertexShaderInputStream,
			InputStream pixelShaderInputStream, Map<String, String> functions) {
		super(vertexShaderInputStream, pixelShaderInputStream, functions);
	}
	
	@Override
	protected String getVersion() {
		return "#version 330 core\n#extension GL_NV_shadow_samplers_cube : enable";
	}
	
	@Override
	protected void onBindAttribLocations(int program) {
		GL20.glBindAttribLocation(program, 0, "ece_Vertex");
	}

	@Override
	protected void onInitializeHandler(int program) {
		textureHandler = GL20.glGetUniformLocation(program, "texture");
	}

	public void glTexture(Texture texture) {
		GL20.glUniform1i(textureHandler, texture.bind(0));
	}

	@Override
	public void clearup() {
		GL20.glDeleteShader(textureHandler);
		super.clearup();
	}
	
}
